//
//  SYClassPlayerViewController.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/10/21.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYClassPlayerViewController.h"
#import "SYClassPlayerCollectionViewCell.h"
#import "SYPlayerViewController.h"

@interface SYClassPlayerViewController () <UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *curCollectionView;
@property (weak, nonatomic) IBOutlet UIButton *fanhuiBtn;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;

@property (strong, nonatomic) SYClassPlayerViewController *classPlayerVC;
@property (weak, nonatomic) IBOutlet UILabel *notLab;



@end

@implementation SYClassPlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    _curCollectionView.delegate = self;
    _curCollectionView.dataSource = self;
    
    [_curCollectionView registerNib:[UINib nibWithNibName:@"SYClassPlayerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"SYClassPlayerCollectionViewCell"];
    if (self.type == SYClassPlayerTutorTypeUp) {
        [_curCollectionView headerRefreshBlock:^{
            
            [self requestDataPayer];
        }];
        
    }
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.type == SYClassPlayerTutorTypeUp) {
       
        self.fanhuiBtn.hidden = YES;
        self.titleLab.text = @"上课视频";
    } else {
        self.fanhuiBtn.hidden = NO;
        self.titleLab.text = self.titleName;
    }
    
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.type == SYClassPlayerTutorTypeUp) {
       
        self.fanhuiBtn.hidden = YES;
        self.titleLab.text = @"上课视频";
    } else {
        if (self.dataArr.count == 0) {
            self.notLab.hidden = NO;
        } else {
            self.notLab.hidden = YES;
        }
        self.fanhuiBtn.hidden = NO;
        self.titleLab.text = self.titleName;
    }
}




- (void)viewDidSlidInto {
    if (self.dataArr.count > 0) {
        
        return;
    }
    [MBProgressHUD mj_showLoadingInView:self.view];
    [self requestDataPayer];
    
}

- (void)requestDataPayer {
    if (self.type != SYClassPlayerTutorTypeUp) {
        [NSObject executeRunloop:^{
            [self.curCollectionView.mj_header endRefreshing];
        } afterDelay:0.5];
        return;
    }
    
    
    [SYRequestAPI zy_requestTutorClassServiceTutorId:@"0" success:^(NSMutableArray *listArr) {
        [self.curCollectionView.mj_header endRefreshing];
        [MBProgressHUD mj_hideHUDForView:self.view];
        [self.dataArr removeAllObjects];
        self.dataArr = listArr;
        if (self.dataArr.count == 0) {
            self.notLab.hidden = NO;
        } else {
            self.notLab.hidden = YES;
        }
        [self.curCollectionView reloadData];
        
    } fail:^(NSError *error) {
        [self.curCollectionView.mj_header endRefreshing];
        [MBProgressHUD mj_hideHUDForView:self.view];
    }];
}


#pragma mark -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SYClassPlayerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SYClassPlayerCollectionViewCell" forIndexPath:indexPath];
    [cell setPlayerCellTutorModel:self.dataArr[indexPath.row] playerType:self.type];
    
    return cell;
}
/**
 cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(125, 170);
}
/**
 每个分区的内边距（上左下右）
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(50, 40, 50, 40);
    
}
/**
 分区内cell之间的最小行间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 50;
}
/**
 分区内cell之间的最小列间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 40;
}
/**
 点击了某个item
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.type == SYClassPlayerTutorTypeUp) {
        self.classPlayerVC = [[SYClassPlayerViewController alloc] init];
        [self.view addSubview:self.classPlayerVC.view];
        self.classPlayerVC.view.frame = CGRectMake(CGRectGetWidth(self.view.frame)+20, 0, CGRectGetWidth(self.view.frame), APP_HEIGHT - 20);
        SYPlayerTutorModel *tutorModel = self.dataArr[indexPath.row];
        self.classPlayerVC.dataArr = tutorModel.dataTutorRecordArr;
        self.classPlayerVC.type = SYClassPlayerTutorTypeRecord;
        self.classPlayerVC.titleName = tutorModel.title;
        
        [UIView animateWithDuration:kAnimate animations:^{
            [self.classPlayerVC.view mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view.mas_left);
                make.width.mas_equalTo(self.view.mas_width);
                make.height.mas_equalTo(APP_HEIGHT-20);
                make.top.equalTo(self.view.mas_top);
            }];
        } completion:^(BOOL finished) {
            
        }];

    } else if (self.type == SYClassPlayerTutorTypeRecord) {
        self.classPlayerVC = [[SYClassPlayerViewController alloc] init];
        [self.view addSubview:self.classPlayerVC.view];
        self.classPlayerVC.view.frame = CGRectMake(CGRectGetWidth(self.view.frame)+20, 0, CGRectGetWidth(self.view.frame), APP_HEIGHT - 20);
        SYPlayerTutorRecordModel *tutorModel = self.dataArr[indexPath.row];
        self.classPlayerVC.dataArr = tutorModel.dateTutorVideoArr;
        self.classPlayerVC.type = SYClassPlayerTutorTypeVideo;
        self.classPlayerVC.titleName = tutorModel.tutorRecord;
        
        [UIView animateWithDuration:kAnimate animations:^{
            [self.classPlayerVC.view mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view.mas_left);
                make.width.mas_equalTo(self.view.mas_width);
                make.height.mas_equalTo(APP_HEIGHT-20);
                make.top.equalTo(self.view.mas_top);
            }];
        } completion:^(BOOL finished) {
            
        }];
    } else {
        SYPlayerTutorVideoModel *videoModel = self.dataArr[indexPath.row];
        SYPlayerViewController *playerVC = [[SYPlayerViewController alloc] init];
        playerVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
        playerVC.urlStr = videoModel.videoPath;
        [self presentViewController:playerVC animated:YES completion:nil];
    }
    
    
}
- (IBAction)fanhuiBtnClick:(UIButton *)sender {
    [UIView animateWithDuration:kAnimate animations:^{
        self.view.frame = CGRectMake(APP_WIDTH, 0, APP_WIDTH, APP_HEIGHT-20);
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
